package com.ease.framework.gef4p.figures.impl.edge {
   import com.ease.component.controls.HandlesMouseCursors;
   import com.ease.framework.figures.IFigure;
   import com.ease.framework.gef4p.figures.edge.IEdgeAnchor;
   import com.ease.framework.gef4p.figures.edge.IEdgeAnchorListener;
   import com.ease.framework.gef4p.figures.edge.IEdgeFigure;

   import flash.events.MouseEvent;
   import flash.geom.Point;

   import mx.collections.ArrayCollection;
   import mx.core.UIComponent;

   public class EdgeAnchor extends UIComponent implements IEdgeAnchor {
      public var edge:IEdgeFigure;
      private var listeners:ArrayCollection = new ArrayCollection();

      //记录当前Edge Anchor的类型： Source or Target
      private var locationType:String;

      //记录当前Edge Anchor的 Stage坐标.
      private var stageLocation:Point;

      public function EdgeAnchor() {
         super();
         this.width = 4;
         this.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
         this.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
      }

      protected function onMouseOver(event:MouseEvent):void {
         cursorManager.removeCursor(cursorManager.currentCursorID);
         cursorManager.setCursor(HandlesMouseCursors.sizeAll, 2, -5, -8);
      }

      protected function onMouseOut(event:MouseEvent):void {
         cursorManager.removeCursor(cursorManager.currentCursorID);
      }

      public function getLocationStagePoint():Point {
         return stageLocation;
      }

      public function setLocationStagePoint(point:Point):void {
         stageLocation = point;
      }

      public function getLocationType():String {
         return locationType;
      }

      public function setLocationType(type:String):void {
         locationType = type;
      }

      public function setEdge(ef:IEdgeFigure):void {
         this.edge = ef;
      }

      public function getEdge():IEdgeFigure {
         return edge;
      }

      public function fireAnchorMoved():void {
         for each (var listener:IEdgeAnchorListener in listeners) {
            listener.archorMoved(this);
         }
      }

      public function registerListener(listener:IEdgeAnchorListener):void {
         if (!listeners.contains(listener)) {
            listeners.addItem(listener);
         }
      }

      public function removeListener(listener:IEdgeAnchorListener):void {
         if (listeners.contains(listener)) {
            listeners.removeItemAt(listeners.getItemIndex(listener));
         }
      }

      override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void {
         super.updateDisplayList(unscaledWidth, unscaledHeight);
         graphics.clear();

         graphics.lineStyle(1, 0xFD5D5E, 0.9);
         graphics.beginFill(0xFFDFDF, 0.9);

         if (this.stageLocation != null) {
            var point:Point = this.globalToLocal(this.stageLocation);

            graphics.drawCircle(point.x, point.y, this.width);
            graphics.endFill();
         }
      }

      public function findFigureAt(stageX:Number, stageY:Number):IFigure {
         return this;
      }
   }
}